package com.uaijug.dengue.core;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

@PersistenceCapable
public class Foto implements BaseBean<com.uaijug.dengue.entidade.Foto> {

	private static final long serialVersionUID = 1L;


	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private BlobKey fotoData;
	@Persistent
	private String contentType;
	@Persistent
	private String fotoPath;
	@Persistent
	private Date dataCaptura;
	@Persistent
	private Foco foco;
	@Persistent
	private Resolucao resolucao;

	public BlobKey getFotoData() {
		return fotoData;
	}

	public void setFotoData(BlobKey fotoData) {
		this.fotoData = fotoData;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFotoPath() {
		return fotoPath;
	}

	public void setFotoPath(String fotoPath) {
		this.fotoPath = fotoPath;
	}

	public Date getDataCaptura() {
		return dataCaptura;
	}

	public void setDataCaptura(Date dataCaptura) {
		this.dataCaptura = dataCaptura;
	}

	@Override
	public com.uaijug.dengue.entidade.Foto convertToDTO() {
		BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService();
		com.uaijug.dengue.entidade.Foto foto = new com.uaijug.dengue.entidade.Foto();
		foto.setId(getKey() != null ? getKey().getId() : null);
		foto.setContentType(getContentType());
		if (getFotoData() != null) {
			BlobInfo info = new BlobInfoFactory().loadBlobInfo(getFotoData());
			foto.setContentType(info.getContentType());
			foto.setDataCaptura(info.getCreation());
			byte[] bytes = service.fetchData(getFotoData(), 0, info.getSize());
			ImagesService imagesService = ImagesServiceFactory.getImagesService();
			Image oldImage = ImagesServiceFactory.makeImage(bytes);
			Transform resize = ImagesServiceFactory.makeResize(100, 100);
			Image newImage = imagesService.applyTransform(resize, oldImage);
			byte[] newImageData = newImage.getImageData();

			foto.setFotoData(newImageData);
		}
		return foto;
	}

	@Override
	public void convertToBean(com.uaijug.dengue.entidade.Foto objet) {
		if (objet != null) {
			if (objet.getId() != null) {
				setKey(KeyFactory.createKey(Foco.class.getSimpleName(), objet.getId()));
			}
			if (objet.getFotoPath() != null && !objet.getFotoPath().equals("")) {
				fotoData = new BlobKey(objet.getFotoPath());
			}
			this.contentType = objet.getContentType();
			this.dataCaptura = objet.getDataCaptura();
		}
	}

	@Override
	public Key getKey() {
		return key;
	}

	@Override
	public void setKey(Key key) {
		this.key = key;
	}

	public Foco getFoco() {
		return foco;
	}

	public void setFoco(Foco foco) {
		this.foco = foco;
	}

	public Resolucao getResolucao() {
		return resolucao;
	}

	public void setResolucao(Resolucao resolucao) {
		this.resolucao = resolucao;
	}
}
